import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from case001.j1 import Mysql01

# 设置浏览器驱动
driver=webdriver.Edge()
# 启动浏览器网址
driver.get('http://10.59.9.12:7001/')
driver.maximize_window()
time.sleep(2)
# 点击登录
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
time.sleep(2)
# 断言登录页面
dy1 = driver.find_element(By.CLASS_NAME,'reg-new').text
assert '会员登录' == dy1
print(dy1)
# 输入用户名
driver.find_element(By.XPATH,'//input[@type="text" and @name="username"]').send_keys('123455556666')
time.sleep(2)
# 输入密码
driver.find_element(By.XPATH,'//input[@type="password" or @name="password"]').send_keys('123456')
time.sleep(10)
# 点击登录
driver.find_element(By.XPATH,'//a[@class="finish-btn"]').click()
time.sleep(5)
# 登录成功断言积分
dy2 = driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[2]/span[1]').text
dy21 = dy2.replace(' 积分','')
print(dy21)
# 点击首页
driver.find_element(By.CSS_SELECTOR,'.nav-wrap > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)').click()
time.sleep(2)
# 输入框输入搜索内容
driver.find_element(By.CLASS_NAME,'search-input').send_keys('非洲之心')
time.sleep(2)
# 点击搜索
driver.find_element(By.XPATH,'//*[@id="searchForm"]/div[2]/input').click()
time.sleep(2)
# 点击商品
driver.find_element(By.CLASS_NAME,'lazy').click()
time.sleep(2)
# 提取单价
dj = driver.find_element(By.ID,'sell-price-box').text
print("商品单价:",dj)
time.sleep(2)
# 点击立即购买
driver.find_element(By.ID,'buy-cart').click()
time.sleep(2)
# 断言进入订单页面
dy3 = driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print(dy3)
# 点击收货地址
driver.find_element(By.ID,'address-831').click()
time.sleep(2)
# 点击支付方式
driver.find_element(By.NAME,'payment').click()
time.sleep(2)
# 下滑页面
driver.execute_script('window.scrollTo(0,2000)')
time.sleep(2)
# 提取商品数量
sl = driver.find_element(By.XPATH,'//*[@id="theForm"]/div[4]/div[1]/table/tbody/tr[2]/td[3]').text
print("商品数量:",sl)
# 提取商品运费
yf = driver.find_element(By.ID,'real-freight').text
yf1 = yf.replace('+','')
print('商品运费:',yf1)
# 提取商品总金额
zje = driver.find_element(By.ID,'order-amount').text
print('商品总金额',zje)
assert float(dj) * float(sl) + float(yf1) == float(zje)
time.sleep(3 )

# 连接数据库
database = Mysql01(host='10.59.9.12', user='root', password='yidong666', database='a3mall')
dyzje = database.select('select point from mall_users where username ="123455556666"')
print('我的积分:',dyzje[0][0])
assert int(dy21) == int(dyzje[0][0])


# 点击下一步
driver.find_element(By.CLASS_NAME,'ask-btn').click()
time.sleep(2)
# 订单完成断言
dy4 = driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print(dy4)



































# # ⑧　获取所有窗口句柄/
# aaa = driver.current_window_handle
# # ⑨　打印获取所有窗口句柄
# print(aaa)
# # ⑩　切换窗口/
# driver.switch_to.window(driver.current_window_handle[-1])

